//
//  BshopZYCPViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/17.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "BshopZYCPViewController.h"
#import "BshopZYCPCollectionViewCell.h"
#import "BshopZYCPmodel.h"

@interface BshopZYCPViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *modelArr;
@property(nonatomic,strong)NSMutableArray *selemodelArr;

@end

@implementation BshopZYCPViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"主营产品";
    [self.leftbutton setImage:imgname(@"left_icon") forState:(UIControlStateNormal)];
    [self.rightbutton setTitleColor:zhutiColor forState:(UIControlStateNormal)];
    [self.rightbutton setTitle:@"确定" forState:(UIControlStateNormal)];
    self.modelArr = [NSMutableArray arrayWithCapacity:1];
    self.selemodelArr = [NSMutableArray arrayWithCapacity:1];

    [self.view addSubview:self.collectionView];
    [self.collectionView reloadData];
    [self requestGoods];
    // Do any additional setup after loading the view.
}

-(void)rightClick{
    if (self.selemodelArr.count == 0) {
        ShowToastWithText(@"至少选择一个主营产品");
        return;
    }
    NSString *name = @"";
    NSString *idstr = @"";

    for (BshopZYCPmodel *model in _selemodelArr) {
        name = [NSString stringWithFormat:@"%@,%@",name,model.type_name];
        idstr = [NSString stringWithFormat:@"%@,%@",idstr,model.Id];
    }
    name  = [name substringFromIndex:1];
    idstr  = [idstr substringFromIndex:1];
    if (self.myblock) {
        self.myblock(name, idstr);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)requestGoods {
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    
    [NetWorkTool POST:BshopgetMainPros param:param success:^(id dic) {
        NSArray *myarr = [dic objectForKey:@"data"];
        for (NSDictionary *mydic in myarr) {
            [DYModelMaker DY_makeModelWithDictionary:mydic modelKeyword:@"" modelName:@""];
            BshopZYCPmodel *model = [[BshopZYCPmodel alloc]init];
            [model setValuesForKeysWithDictionary:mydic];
            model.isele = @"0";
            [self.modelArr addObject:model];
        }
        [self.collectionView reloadData];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        
    } fail:^(NSError *error) {
        
    } needUser:NO];
}

#pragma mark - CollectionView -

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    BshopZYCPCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BshopZYCPCollectionViewCell" forIndexPath:indexPath];
    
    BshopZYCPmodel *model = [_modelArr safeObjectAtIndex:indexPath.row];
    cell.myconLb.text =model.type_name;
    cell.myconLb.backgroundColor = [UIColor clearColor];
    if ([model.isele integerValue] == 1) {
        cell.myimae.image = imgname(@"selezycpddd");
    } else {
        cell.myimae.image = imgname(@"Unselezycpddd");
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath {
    BshopZYCPmodel *model = [_modelArr safeObjectAtIndex:indexPath.row];
    if ([self.selemodelArr containsObject:model]) {
        model.isele = @"0";
        [self.selemodelArr removeObject:model];
        [self.collectionView reloadData];
    } else {
        model.isele = @"1";
        [self.selemodelArr addObject:model];
        [self.collectionView reloadData];
    }
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake((KWIDTH-32-30)/4 , 49);
        //layout.headerReferenceSize = CGSizeMake(KWIDTH, 204);
        layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, kNaviHeight, KWIDTH, KHEIGHT-kNaviHeight) collectionViewLayout:layout];
        adjustInset(_collectionView);
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        _collectionView.backgroundColor =[UIColor whiteColor];
        
        [_collectionView registerNib:[UINib nibWithNibName:@"BshopZYCPCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"BshopZYCPCollectionViewCell"];
        [_collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
        [_collectionView reloadData];
        
    }
    return _collectionView;
}

@end
